package com.zhike.service.serviceimpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.zhike.mapper.SampleMapper;
import com.zhike.model.NewMessage;
import com.zhike.model.Project;
import com.zhike.model.Sample;
import com.zhike.model.Standard;
import com.zhike.service.SampleService;
import com.zhike.util.BasicsUtil;
import com.zhike.util.DataSourceEnum;
import com.zhike.util.DataSourceHolder;
import com.zhike.util.DateUtil;
import com.zhike.util.JsonUtil;
@Service
public class SampleServiceImpl implements SampleService{

	@Resource
	SampleMapper sampleMapper;
	
	@Override
	public String addEntrustment(Sample sample) {
		DataSourceHolder.setDataSources(DataSourceEnum.DS1.getKey());
		String currentTime = DateUtil.getCurrentTime();
		sample.setRegisterDate(currentTime);
		sampleMapper.addEntrustment(sample);
		return JsonUtil.blank(true, 200, "新增委托成功");			
	}

	@Override
	public String entrustmentList(Sample sample) {
		DataSourceHolder.setDataSources(DataSourceEnum.DS1.getKey());
		JsonUtil js = new JsonUtil();
		List<Sample> list = new ArrayList<>();
		list = sampleMapper.entrustmentList(sample);
		

		 Collections.sort(list); 
		
		//存进list之后进行分页
		List<Sample> listBeanPage = new ArrayList<>();
        int currIdx = (sample.getPage() > 1 ? (sample.getPage() -1) * sample.getPageSize() : 0);
        for (int i = 0; i < sample.getPageSize() && i < list.size() - currIdx; i++) {
        	Sample nMessage = list.get(currIdx + i);
        	listBeanPage.add(nMessage);
        }
		js.setList(listBeanPage);
		return js.getJson();
	}

	@Override
	public String entrustmentExamine(Sample sample) {
		DataSourceHolder.setDataSources(DataSourceEnum.DS1.getKey());
		JsonUtil js = new JsonUtil();
		Sample s = sampleMapper.entrustmentExamine(sample);
		js.add(s);
		return js.getJson();
	}

	@Override
	public String sampleList(Sample sample) {
		DataSourceHolder.setDataSources(DataSourceEnum.DS1.getKey());
		JsonUtil js = new JsonUtil();
		List<Sample> list = new ArrayList<>();
		list = sampleMapper.sampleList(sample);
		js.setList(list);
		return js.getJson();
	}

	@Override
	public String standardOut(Standard standard) {
		DataSourceHolder.setDataSources(DataSourceEnum.DS1.getKey());
		JsonUtil js = new JsonUtil();
		List<Standard> list = new ArrayList<>();
		list =  sampleMapper.standardOut(standard);
		js.setList(list);
		return js.getJson();
	}

	@Override
	public String standardMiddle(Standard standard) {
		DataSourceHolder.setDataSources(DataSourceEnum.DS1.getKey());
		JsonUtil js = new JsonUtil();
		List<Standard> list = new ArrayList<>();
		list =  sampleMapper.standardMiddle(standard);
		js.setList(list);
		return js.getJson();
	}

	@Override
	public String standardInner(Standard standard) {
		DataSourceHolder.setDataSources(DataSourceEnum.DS1.getKey());
		JsonUtil js = new JsonUtil();
		List<Standard> list = new ArrayList<>();
		list =  sampleMapper.standardInner(standard);
		js.setList(list);
		return js.getJson();
	}

	@Override
	public String sampleMessage(Sample sample) {
		DataSourceHolder.setDataSources(DataSourceEnum.DS1.getKey());
		sampleMapper.sampleMessage(sample);
		return JsonUtil.blank(true, 200, "调用接口成功");
	}

	@Override
	public String sampleUse(Sample sample) {
		DataSourceHolder.setDataSources(DataSourceEnum.DS1.getKey());
		//把传递过来的ids 转换为list
		List<String> list = Arrays.asList(StringUtils.split(sample.getIds(),","));
		for (int i = 0; i < list.size(); i++) {
			sample.setId(Integer.parseInt(list.get(i)));
			sampleMapper.sampleUse(sample);
		}
		return JsonUtil.blank(true, 200, "调用接口成功");
	}

	@Override
	public String sampleStateOne(Sample sample) {
		DataSourceHolder.setDataSources(DataSourceEnum.DS1.getKey());
		JsonUtil jsonUtil = new JsonUtil();
		List<Sample> list = new ArrayList<>();
		if(sample.getSampleState()!=null || !sample.getSampleState().equals("")) {
			//状态不为空 或者不等于null时 用这个数据库操作
			list = sampleMapper.sampleStateOne(sample);
		}
		if(sample.getSampleState()==null || sample.getSampleState().equals("")) {
			list = sampleMapper.sampleStatetwo(sample);
		}
		jsonUtil.setList(list);
		return jsonUtil.getJson();
	}

	@Override
	public String sampleEmploy(Sample sample) {
		DataSourceHolder.setDataSources(DataSourceEnum.DS1.getKey());
		sampleMapper.sampleEmploy(sample);
		return JsonUtil.blank(true, 200, "接口调用成功");
	}

	@Override
	public String sampleManage(Sample sample) {
		DataSourceHolder.setDataSources(DataSourceEnum.DS1.getKey());
		sampleMapper.sampleManage(sample);
		return JsonUtil.blank(true, 200, "接口调用成功");
	}

	@Transactional
	@Override
	public String projectApproval(Project project) {
		DataSourceHolder.setDataSources(DataSourceEnum.DS1.getKey());
	//设置当前时间
	String currentTimeTwo = DateUtil.getCurrentTime();
	project.setApprovalDate(currentTimeTwo);
	//获取当前年月日
	String currentYear = DateUtil.getCurrentDate();
	//查询最后 一条项目编号
	String str = sampleMapper.getProjectNumber();
	String projectNumber ="0001";
	
	if (str.contains(currentYear)) {
		String substring = str.substring(str.length()-4,str.length());
		Integer xMnumber = Integer.valueOf(substring);
		
		//项目编号
		projectNumber = BasicsUtil.projectNumberUtil(xMnumber);
	}
		
	

				
	if (project.getDeptType().equals("至科地基")) {
		project.setProjectNumber("ZKDJ"+currentYear+projectNumber);
	}	
	if (project.getDeptType().equals("至科机构")) {
		project.setProjectNumber("ZKJG"+currentYear+projectNumber);
	}	
	if (project.getDeptType().equals("至科试验室")) {
		project.setProjectNumber("ZKSYS"+currentYear+projectNumber);
	}	
	sampleMapper.projectApproval(project);
	return JsonUtil.blank(true, 200, "接口调用成功");
	}

	@Override
	public String projectList(Project project) {
		DataSourceHolder.setDataSources(DataSourceEnum.DS1.getKey());
		JsonUtil jsonUtil = new JsonUtil();
		List<Project> list = new ArrayList<>();
		list = sampleMapper.projectList(project);
		
		
		//list时间戳
		for (int i = 0; i < list.size(); i++) {
			Project newMessage = list.get(i);
			newMessage.setApprovalDate(newMessage.getApprovalDate().substring(0, 19));
		}
		
		DateUtil.ListSortNS(list);
		List<Project> listBeanPage = new ArrayList<>();
        int currIdx = (project.getPage() > 1 ? (project.getPage() -1) * project.getPageSize() : 0);
        for (int i = 0; i < project.getPageSize() && i < list.size() - currIdx; i++) {
        	Project nMessage = list.get(currIdx + i);
        	listBeanPage.add(nMessage);
        }
        jsonUtil.setList(listBeanPage);
		return jsonUtil.getJson();
	}

}
